﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _2_11
{
    public partial class Form1 : Form
    {
        private int[] num;
        private int[] prime;
        private Random random = new Random();
        public Form1()
        {
            InitializeComponent();
            btnPrimes.Enabled = false; // 初始时禁用素数按钮
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private bool IsPrime(int n)
        {
            if (n < 2) return false;
            if (n == 2) return true;
            if (n % 2 == 0) return false;

            int boundary = (int)Math.Sqrt(n);

            for (int i = 3; i <= boundary; i += 2)
            {
                if (n % i == 0)
                    return false;
            }

            return true;
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            try
            {
                int count = int.Parse(txtCount.Text);
                int min = int.Parse(txtMin.Text);
                int max = int.Parse(txtMax.Text);

                if (min >= max)
                {
                    MessageBox.Show("最小值必须小于最大值！");
                    return;
                }

                num = new int[count];
                List<int> primeList = new List<int>();

                for (int i = 0; i < count; i++)
                {
                    num[i] = random.Next(min, max + 1);
                    if (IsPrime(num[i]))
                        primeList.Add(num[i]);
                }

                prime = primeList.ToArray();

                // 显示所有随机数
                txtNumbers.Text = string.Join(Environment.NewLine, num);

                // 启用素数按钮
                btnPrimes.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("请输入有效的数值！");
            }
        }

        private void btnPrimes_Click(object sender, EventArgs e)
        {
            if (prime == null) return;

            txtPrimes.Text = string.Join(Environment.NewLine, prime);
        }
    }
}
